package com.wgn.reportquery.utils;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.google.gson.Gson;
import com.wgn.reportquery.cons.Const;
import com.wgn.reportquery.model.ReportDate;
import com.wgn.reportquery.model.ReportInfo;
import com.wgn.reportquery.model.Reportdata;
import com.wgn.reportquery.rj.model.Indicatorsresult;
import com.wgn.reportquery.rj.model.RJQueryReports;
import com.wgn.reportquery.rj.model.RJReport;
import com.wgn.reportquery.rj.model.RJReportInfo;

public class ReportUtils {
	final static String cardsselecthtmlstr = "<option value='##'>##</option>";

	// ['January', 'February', 'March', 'April', 'May', 'June', 'Jaly']
	public static String listToArray(List<String> list) {
		StringBuffer sf = new StringBuffer();
		sf.append("[");
		int index = 0;
		if (null != list)
			for (String s : list) {
				if (0 != index++)
					sf.append(",");
				sf.append("'").append(s).append("'");
			}
		if (0 == index)
			sf.append(0);
		sf.append("]");
		return sf.toString();
	}

	/*
	 * <li data-filter-camera-type="all"><a data-toggle="tab"
	 * href="#">HTML5</a></li> <li data-filter-camera-type="all"><a
	 * data-toggle="tab" href="#">HTML6</a></li> <li
	 * data-filter-camera-type="all"><a data-toggle="tab"
	 * href="#">HTML7</a></li>
	 */
	static String dropHtmlStringhead = "<li data-filter-camera-type='all'><a class='xz' data-toggle='tab' href='#'>";
	static String dropHtmlStringtail = "</a></li>";

	public static String getHtml(List<String> list) {
		// TODO Auto-generated method stub
		StringBuffer sf = new StringBuffer();
		for (String s : list) {
			sf.append(dropHtmlStringhead).append(s).append(dropHtmlStringtail);
		}
		return sf.toString();
	}

	public static String getHtmlTable(List<String> list) {
		// TODO Auto-generated method stub
		StringBuffer sf = new StringBuffer();
		int index = 0;
		int rowNum = 1;
		for (String s : list) {
			if (0 == index || index % 5 == 0)
				sf.append("<tr>");
			sf.append("<td><a>").append(s).append("</a></td>");
			if (index == (5 * rowNum - 1)) {
				sf.append("</tr>");
				++rowNum;
			}
			++index;
		}
		if (list.size() > 0 && list.size() != 5 * rowNum)
			sf.append("</tr>");
		return sf.toString();
	}

	public static Object str2Object(String json, Class objectClass) {

		/**
		 * 下面是json直接通过gson转换为实例对象
		 */
		// json to javabean
		Gson gson = new Gson();
		return gson.fromJson(json, objectClass);// 对于javabean直接给出class实例

		// String json = "{\"username\":\"张三\",\"password\":\"123\"}";

	}

	public static String obj2gson(Object rjq) {
		// TODO Auto-generated method stub
		Gson gson = new Gson();
		return gson.toJson(rjq);
	}

	public static String getYears() {
		// TODO Auto-generated method stub
		Calendar now = Calendar.getInstance();
		int thisyear = now.get(Calendar.YEAR);
		int lastyear = thisyear - 1;
		StringBuilder s = new StringBuilder();
		s.append("<option>").append(thisyear).append("</option>");
		s.append("<option>").append(lastyear).append("</option>");
		return s.toString();
	}

	public static ReportDate getDate(String split) {
		// TODO Auto-generated method stub
		ReportDate date = new ReportDate();
		Calendar now = Calendar.getInstance();
		int thisyear = now.get(Calendar.YEAR);
		int thismonth = now.get(Calendar.MONTH) + 1;
		int thisday = now.get(Calendar.DAY_OF_MONTH);
		int lastyear = thisyear - 1;
		StringBuilder s = new StringBuilder();
		String month = String.valueOf(thismonth);
		if (thismonth < 10)
			month = "0" + month;
		String day = String.valueOf(thisday);
		if (thisday < 10)
			day = "0" + day;
		s = new StringBuilder();
		s.append(lastyear).append(split).append(month).append(split)
				.append(day);
		date.setStart(s.toString());
		now.add(Calendar.DATE, 2); // 得到后天
		DateFormat df = new SimpleDateFormat("yyyy" + split + "MM" + split
				+ "dd");
		Date date_end = now.getTime();
		date.setEdn(df.format(date_end));

		return date;
	}

	public static String getMonths() {
		// TODO Auto-generated method stub
		Calendar now = Calendar.getInstance();
		int thismonth = now.get(Calendar.MONTH) + 1;
		StringBuilder s = new StringBuilder();
		s.append("<option>").append(thismonth).append("</option>");
		for (int i = 1; i < 12; i++) {
			if (i == thismonth)
				continue;
			s.append("<option>").append(i).append("</option>");
		}
		return s.toString();
	}

	public static String getDays() {
		// TODO Auto-generated method stub
		Calendar now = Calendar.getInstance();
		int thisday = now.get(Calendar.DAY_OF_MONTH);
		StringBuilder s = new StringBuilder();
		s.append("<option>").append(thisday).append("</option>");
		for (int i = 1; i < 32; i++) {
			if (i == thisday)
				continue;
			s.append("<option>").append(i).append("</option>");
		}
		return s.toString();
	}

	/*
	 * 1正常 3偏高 其他偏低
	 */
	public static String getVs(String input) {
		if (null == input || "".endsWith(input))
			return "";
		if ("1".endsWith(input))
			return "<font color='green'>正常</font>";
		else if ("3".endsWith(input))
			return "<font color='red'>偏高</font>";
		else
			return "<font color='blue'>偏低</font>";
	}

	public static String getCardStr(List<String> list) {
		// TODO Auto-generated method stub
		StringBuffer sf = new StringBuffer();
		for (String s : list) {
			sf.append(cardsselecthtmlstr.replaceAll("##", s));
		}
		return sf.toString();
	}

	public static String getRJQuerListHtml(List<RJReportInfo> rss) {
		StringBuffer s = new StringBuffer();
		for (RJReportInfo r : rss) {
			s.append(
					"<tr onclick=\"javascript:window.location.href='dosomequery?querytype=RJQuerReport&bgnum="
							+ r.getBgdh() + "'\"><td>").append(r.getBgdh())
					.append("</td><td>").append(r.getBbmc())
					.append("</td><td>").append(r.getBgdlb())
					.append("</td><td>").append(r.getBgrq())
					.append("</td></tr>").append("</a>");
		}
		return s.toString();
	}

	/*
	 * public static String getSKQuerListHtml(List<ReportInfo> rss) {
	 * StringBuffer s = new StringBuffer(); for (ReportInfo r : rss) { s.append(
	 * "<tr onclick=\"javascript:window.location.href='dosomequery?querytype=SKQuerReport&bgnum="
	 * + r.getBgdh() + "'\"><td>").append(r.getBgdh())
	 * .append("</td><td>").append(r.getBbmc())
	 * .append("</td><td>").append(r.getBgdlb())
	 * .append("</td><td>").append(r.getBgrq())
	 * .append("</td></tr>").append("</a>"); } return s.toString(); }
	 */
	/*
	 * 仁济编号20170523-SH01-1-G0561170-E 申康编号20170110G0561145 只匹配G0561170 也就是第四个字段
	 */
	public static String getRjNo(String rjNo) {
		// TODO Auto-generated method stub
		String[] array = rjNo.split("-");

		return array[3];
	}

	public static String getRJReportHtml(RJReport rs) {
		StringBuffer s = new StringBuffer();
		if (null != rs)
			for (Indicatorsresult r : rs.getReport().getIndicatorsresult()) {
				s.append("<tr><td class='zbmc'>").append(r.getJczbmc())
						.append("</td><td>").append(r.getJczbjg())
						.append("</td><td>").append(r.getCkz())
						.append("</td><td>")
						.append(ReportUtils.getVs(r.getYcts()))
						.append("</td><td>").append(r.getDw())
						.append("</td></tr>");
			}
		return s.toString();
	}

	public static List<Reportdata> transRjReportData(RJReport rp,
			String reportdate, String reportNo) {
		// TODO Auto-generated method stub
		List<Reportdata> rd = new ArrayList();
		List<Indicatorsresult> list = rp.getReport().getIndicatorsresult();
		int index = 0;
		for (Indicatorsresult i : list) {
			String no = String.valueOf(index++);
			String info = i.getJczbmc();
			String val = i.getJczbjg();
			String scope = i.getCkz();
			String dw = i.getDw();
			String jg = i.getYcts();
			Reportdata r = new Reportdata(no, info, val, scope, reportdate,
					reportNo, dw, jg);
			rd.add(r);
		}
		return rd;
	}

	public static boolean isWindows() {
		String osName = System.getProperty("os.name").toLowerCase();
		if (osName.indexOf("window") != -1)
			return true;
		else
			return false;
	}

	public static String getChinese(String input) {
		String output = "";
		try {
			if (isWindows()) {

				output = java.net.URLDecoder.decode(input.replaceAll("%", "%25"), "UTF-8");
			} else {
				output = new String(input.getBytes("iso-8859-1"), "utf-8");
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return output;
	}

	public static String getSkCodeImgUrl(String username) {
		// TODO Auto-generated method stub
		String dir = Const.TMPDIR;
		if (!isWindows())
			dir = Const.tmpdir_ubuntu;
		return "img/" + username + ".jpg";
	}

	public static ReportDate getNextPreDate() {
		ReportDate rd = new ReportDate();
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, -1); // 得到前一天
		Date date = calendar.getTime();
		DateFormat df = new SimpleDateFormat("yyyyMMdd");
		// System.out.println(df.format(date));
		rd.setStart(df.format(date));
		// 得到后一天
		calendar.add(Calendar.DATE, 2); // 得到后天
		Date date_end = calendar.getTime();
		rd.setEdn(df.format(date_end));
		// System.out.println(df.format(date_end));
		return rd;
	}
}
